package top.callname.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import top.callname.bean.AdminBean;
import top.callname.bean.RspInfoBean;
import top.callname.service.SignService;
import top.callname.service.impl.SignServiceImpl;
import top.callname.util.BaseUtil;


@WebServlet("/admin/report")
public class ReportServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
  
	private SignService signService = new SignServiceImpl();

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String method = request.getParameter("method");
		AdminBean admin = (AdminBean) request.getSession().getAttribute("adminInfo");
		//查询所有的 TODO 待做
		request.setAttribute("signList", signService.getListPageByPid(1,admin.getPid()).getList());
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
	     if("signYes".equals(method)){ 
	    	 
			String pageNo = request.getParameter("pageNo");
			String signId =  request.getParameter("signId");
			int page = BaseUtil.getPageNo(pageNo);
			request.setAttribute("page", signService.getYesSignListBySignId(page, Integer.valueOf(signId)) );
			request.setAttribute("currentSignId", signId);
			request.getRequestDispatcher("report-yes.jsp").forward(request,response);
			
		}else if("signNo".equals(method)){ 
			
			String pageNo = request.getParameter("pageNo");
			String signId =  request.getParameter("signId");
			int page = BaseUtil.getPageNo(pageNo);
			request.setAttribute("page", signService.getNoSignListBySignId(page, Integer.valueOf(signId)) );
			request.setAttribute("currentSignId", signId);
			request.getRequestDispatcher("report-no.jsp").forward(request,response);
			
		}else if("addSignRecord".equals(method)){ 
			
			//将用户加入到已签到
			String signId = request.getParameter("signId");
			String stuNo =  request.getParameter("stuNo");
			RspInfoBean rsp =  signService.signIngByAdmin(stuNo, Integer.valueOf(signId));
			out.write(JSON.toJSONString(rsp));
				
		}else if("removeSignRecord".equals(method)){ 
			
			//将用户签到记录删除
			String rid = request.getParameter("rid");
			RspInfoBean rsp =  signService.removeSignRecord(Integer.valueOf(rid));
			out.write(JSON.toJSONString(rsp));
			
		}else if("export".equals(method)){ 
			//导出统计结果
			String signId =  request.getParameter("signId");
			response.reset(); 
			signService.exportSignRecord(request, response, Integer.valueOf(signId));
			
		}else{
			request.getRequestDispatcher("report-yes.jsp").forward(request,response);
		}
	     
	     
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	

}
